package com.googlecode.geoteacher;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import org.json.JSONArray;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.googlecode.geoteacher.db.DataSource;
import com.googlecode.geoteacher.db.model.Challenge;
import com.googlecode.geoteacher.logic.ChallengeFile;
import com.googlecode.geoteacher.logic.ChallengeFileReader;
import com.googlecode.geoteacher.util.AssetsUtils;
import com.googlecode.geoteacher.util.JsonUtils;

public class SinglePlayerActivity extends Activity implements
		OnItemClickListener {

	private DataSource datasource;

	private List<Challenge> challengeList;

	private ListView challengeListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_singleplayer);

		datasource = new DataSource(this);
		datasource.open();

		challengeListView = (ListView) findViewById(R.id.challenge_list);

		initDB();
		initUI();
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initDB() {
		if (datasource.countChallenges() == 0) {
			try {
				InputStream in = getAssets().open("challenge_list.json");
				JSONArray array = JsonUtils.loadArray(in);
				in.close();

				for (int i = 0, len = array.length(); i < len; ++i) {

					URL url = AssetsUtils.getAssetURL(array.getString(i));

					in = getAssets().open(array.getString(i));
					ChallengeFile challengeFile = ChallengeFileReader.read(in);
					in.close();

					Challenge challenge = new Challenge(challengeFile.getHash(),
							challengeFile.getName(), url.toString());

					datasource.createChallenge(challenge);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void initUI() {
		challengeList = datasource.getAllChallenges();

		Log.i(SinglePlayerActivity.class.getName(), challengeList.toString());

		ArrayAdapter<Challenge> adapter = new ArrayAdapter<Challenge>(this,
				android.R.layout.simple_list_item_1, challengeList) {

			public View getView(int position, View convertView, ViewGroup parent) {
				View view;
				TextView text;

				if (convertView == null) {
					view = getLayoutInflater().inflate(
							android.R.layout.simple_list_item_1, parent, false);
				} else {
					view = convertView;
				}

				// If no custom field is assigned, assume the whole
				// resource is a TextView
				text = (TextView) view;

				text.setText(getItem(position).getName());

				return view;
			}

		};

		challengeListView.setAdapter(adapter);
		challengeListView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Intent intent = new Intent(SinglePlayerActivity.this, MapActivity.class);
		intent.putExtra(MapActivity.INTENT_CHALLENGE_ID,
				challengeList.get(position).getId());
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		startActivity(intent);
	}
}
